/**
 * 
 */
package br.com.ctm.advogadope.model;

import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

import br.com.ctm.advogadope.constraints.Email;
import br.com.framework.persistencia.model.AuditedEntity;

/**
 * @author Cleber
 *
 */
@Entity
@Table(name = "USUARIO")
@XmlRootElement
public class Usuario extends AuditedEntity {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String email;
	private String senha;
	
	private transient String confirmaSenha;
	
	private Cliente cliente;
	
	private Set<Papel> papeis;
	
	private String uuidLogado;
	
	/**
	 * 
	 */
	public Usuario() {
	}
	
	@Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="SEQ_USUARIO")
	@SequenceGenerator(name="SEQ_USUARIO", sequenceName="SEQ_USUARIO", allocationSize=1, initialValue=0)
	@Column(name = "ID_USUARIO")
	public Long getId() {
		return super.getId();
	}
	
	/**
	 * @return the email
	 */
	@Column(name = "EMAIL", length = 255, nullable = false, unique = true)
	@NotNull(message = "{geral.campo.email.obrigatorio}")
	@Email(message = "{geral.campo.email.invalido}")
	public String getEmail() {
		return email;
	}

	/**
	 * @param email the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return the senha
	 */
	@Column(name = "SENHA", length = 255, nullable = false)
	@Size(min = 3, max = 255, message = "{usuario.senha.tamanhoInvalido}")
	public String getSenha() {
		return senha;
	}

	/**
	 * @param senha the senha to set
	 */
	public void setSenha(String senha) {
		this.senha = senha;
	}

	
	@ManyToMany(targetEntity=Papel.class)
	@JoinTable(name = "USUARIO_PAPEL", 
		joinColumns = @JoinColumn(name = "ID_USUARIO", referencedColumnName = "ID_USUARIO"), 
		inverseJoinColumns = @JoinColumn(name = "ID_PAPEL", referencedColumnName = "ID_PAPEL"))
	public Set<Papel> getPapeis() {
		return papeis;
	}
	
	public void setPapeis(Set<Papel> papeis) {
		this.papeis = papeis;
	}

	/**
	 * @return the cliente
	 */
	@OneToOne(fetch=FetchType.LAZY, mappedBy="usuario")
	public Cliente getCliente() {
		return cliente;
	}

	/**
	 * @param cliente the cliente to set
	 */
	public void setCliente(Cliente cliente) {
		this.cliente = cliente;
	}

	/**
	 * @return the confirmaSenha
	 */
	@Transient
	@Size(min = 3, max = 255, message = "{usuario.senha.tamanhoInvalido}")
	public String getConfirmaSenha() {
		return confirmaSenha;
	}

	/**
	 * @param confirmaSenha the confirmaSenha to set
	 */
	public void setConfirmaSenha(String confirmaSenha) {
		this.confirmaSenha = confirmaSenha;
	}

	/**
	 * @return the uuidLogado
	 */
	@Column(name = "UUID_LOGADO", length = 255, nullable = true)
	public String getUuidLogado() {
		return uuidLogado;
	}

	/**
	 * @param uuidLogado the uuidLogado to set
	 */
	public void setUuidLogado(String uuidLogado) {
		this.uuidLogado = uuidLogado;
	}
	
}
